package com.sinodata.bsm.center.engine.notify;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.sinodata.bsm.center.util.system.SystemContext;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 PM 9:53:36    liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class NotifyConfig {

    private static final Logger logger = Logger.getLogger(NotifyConfig.class);

    private static boolean loaded = false;

    private static List<NotifyConfig> configs = new ArrayList<NotifyConfig>();

    private int id;

    private String name;

    private String type;

    /**
     * 
     */
    public NotifyConfig() {

    }

    public synchronized static List<NotifyConfig> getAlarmConfigs() {
        if (!loaded) {
            load();
        }
        return configs;
    }

    public static String getAlarmDesc(int model) {
        if (!loaded) {
            load();
        }
        for (int i = 0; i < configs.size(); i++) {
            if (configs.get(i).getId() == model) {
                return configs.get(i).getName();
            }
        }
        return "Unknow";
    }

    /**
     * 将xml文件内容放到 list 中
     */
    private static void load() {
        String appContextPath = (String) SystemContext.getProperty("AppContextPath");
        String fileName = appContextPath + "WEB-INF/conf/notify-config.xml";
        SAXReader r = new SAXReader();
        try {
            Document doc = r.read(new File(fileName));
            Element root = doc.getRootElement();
            for (Iterator i = root.elementIterator("modes"); i.hasNext();) {
                Element modes = (Element) i.next();
                for (Iterator k = modes.elementIterator("mode"); k.hasNext();) {
                    Element mode = (Element) k.next();
                    NotifyConfig alarmConfig = new NotifyConfig();
                    for (Iterator j = mode.attributeIterator(); j.hasNext();) {
                        Attribute attribute = (Attribute) j.next();
                        if (attribute.getName().equalsIgnoreCase("id")) {
                            alarmConfig.setId(Integer.valueOf(attribute.getValue()));
                        }
                        if (attribute.getName().equalsIgnoreCase("name")) {
                            alarmConfig.setName(attribute.getValue());
                        }
                        if (attribute.getName().equalsIgnoreCase("type")) {
                            alarmConfig.setType(attribute.getValue());
                        }
                    }
                    configs.add(alarmConfig);
                }
            }
            loaded = true;
        } catch (DocumentException e) {
            loaded = false;
            logger.error("failed to parse alarm config xml", e);
        }
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

}
